package com.lwy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lwy.dao.BookShelfMapper;
import com.lwy.entity.BookNove;
import com.lwy.entity.BookShelf;
import com.lwy.service.impl.BookShelfServiceImpl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
//控制层，可以接受http端口的请求
@RestController
@RequestMapping("shelf")//窄化路径
public class BookShelfController {

    @Resource//按名称注入
    private BookShelfServiceImpl bookShelfService;

    @Resource
    private BookShelfMapper bookShelfMapper;
//    新增到书架
    @RequestMapping("save")
    public boolean save(BookShelf bookShelf){
        return bookShelfService.save(bookShelf);
    }


    @RequestMapping("findById")
    public Object findById(Integer id,Integer user_id){
        List<BookShelf> bookShelves = bookShelfService.list(new QueryWrapper<BookShelf>().eq("novel_id", id).eq("user_id",user_id).eq("status",1));
        if(bookShelves.size()>0){
            return 1;
        }else{
            return 0;
        }
    }


    @RequestMapping("findByList")
    public List<BookShelf> findList(Integer user_id){
        return bookShelfService.findByList(user_id);
    }

    @RequestMapping("listAll")
    public List<BookNove> listAll(){
        return bookShelfService.listAll();
    }

//修改
    @RequestMapping("edit")
    public Object edit(BookShelf bookShelf){
        return bookShelfService.updateById(bookShelf);
    }


    @RequestMapping("findByUid")
    public Integer uid(Integer user_id,Integer novel_id){
        BookShelf byUid = bookShelfMapper.findByUid(user_id, novel_id);
        if(byUid!=null){
            return 1;
        }else{
            return 0;
        }
    }


    @RequestMapping("findBycpt")
    public BookShelf cpt(Integer user_id,Integer novel_id){
       return bookShelfMapper.findByUid(user_id, novel_id);
    }

}
